/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nutmotion.application;

import com.nutmotion.model.bean.ListsBean;
import com.nutmotion.model.bean.UserBean;
import com.nutmotion.model.jpa.User;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 *
 * @author Manu
 * Hat tip BalusC @ stackoverflow ;) http://stackoverflow.com/questions/7862700/best-practice-to-get-entitymanagerfactory
 */
public class EMF implements ServletContextListener {

    private static EntityManagerFactory emf;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        emf = Persistence.createEntityManagerFactory("tradeUplanPU");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        emf.close();
    }

    public static EntityManager createEntityManager() {
        if (emf == null) {
            throw new IllegalStateException("Context is not initialized yet.");
        }

        return emf.createEntityManager();
    }
}